using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RoomGenerator : MonoBehaviour
{
    [Header("Room Prefabs")]
    public List<RoomPrefab> combatRooms;
    public List<RoomPrefab> treasureRooms;
    public List<RoomPrefab> eventRooms;
    public List<RoomPrefab> shopRooms;
    public RoomPrefab bossRoom;

    [Header("Generation Parameters")]
    public int minRoomsPerFloor = 5;
    public int maxRoomsPerFloor = 8;
    public float roomSpacing = 20f;

    public Floor GenerateFloor(int floorNumber, int difficulty)
    {
        Floor newFloor = new Floor(floorNumber, difficulty);

        // 确定房间数量
        int roomCount = UnityEngine.Random.Range(minRoomsPerFloor, maxRoomsPerFloor + 1);

        // 确保有一个boss房间
        Room bossRoom = CreateRoom(RoomType.Boss, difficulty);
        newFloor.AddRoom(bossRoom);

        // 生成其他房间
        for (int i = 0; i < roomCount - 1; i++)
        {
            RoomType type = GetRandomRoomType(floorNumber);
            Room room = CreateRoom(type, difficulty);
            newFloor.AddRoom(room);
        }

        // 布局房间
        LayoutRooms(newFloor);

        // 设置起始房间
        newFloor.SetStartingRoom();

        return newFloor;
    }

    private Room CreateRoom(RoomType type, int difficulty)
    {
        RoomPrefab prefab = GetPrefabForRoomType(type);

        // ʵ��������
        GameObject roomInstance = Instantiate(prefab.prefab);
        Room room = roomInstance.AddComponent<Room>();
        room.Initialize(
            Guid.NewGuid().ToString(),
            type,
            prefab,
            GenerateRoomContent(type, difficulty)
        );

        return room;
    }

    private void LayoutRooms(Floor floor)
    {
        List<Room> rooms = floor.GetAllRooms();
        if (rooms.Count == 0) return;

        // 将第一个房间放在原点
        rooms[0].transform.position = Vector3.zero;

        // 使用圆形布局排列其他房间
        for (int i = 1; i < rooms.Count; i++)
        {
            float angle = (i * (360f / (rooms.Count - 1))) * Mathf.Deg2Rad;
            float distance = roomSpacing * (1 + (i % 3)); // 分散布局

            float x = Mathf.Cos(angle) * distance;
            float z = Mathf.Sin(angle) * distance;

            rooms[i].transform.position = new Vector3(x, 0, z);

            // 创建房间之间的通道
            CreateCorridor(rooms[0].transform.position, rooms[i].transform.position);
        }
    }

    private void CreateCorridor(Vector3 startPos, Vector3 endPos)
    {
        // 创建一个简单的走廊连接两个点
        GameObject corridor = new GameObject("Corridor");
        BoxCollider collider = corridor.AddComponent<BoxCollider>();

        // 设置走廊位置和旋转
        Vector3 midPoint = (startPos + endPos) / 2;
        corridor.transform.position = midPoint;

        Vector3 direction = endPos - startPos;
        corridor.transform.forward = direction.normalized;

        // 设置走廊大小
        float length = direction.magnitude;
        collider.size = new Vector3(5f, 3f, length);
        collider.center = new Vector3(0, 1.5f, length / 2);

        // 添加走廊墙壁，优化性能
        // 实际项目中应使用预制件
    }

    private RoomType GetRandomRoomType(int floorNumber)
    {
        // 根据楼层调整，增加战斗房间概率
        float combatChance = 0.5f + (floorNumber * 0.02f);
        float treasureChance = 0.2f;
        float eventChance = 0.2f;
        float shopChance = 0.1f;

        // 限制最大值
        combatChance = Mathf.Min(combatChance, 0.7f);

        float random = UnityEngine.Random.value;

        if (random < combatChance) return RoomType.Combat;
        random -= combatChance;
        if (random < treasureChance) return RoomType.Treasure;
        random -= treasureChance;
        if (random < eventChance) return RoomType.Event;
        return RoomType.Shop;
    }

    private RoomPrefab GetPrefabForRoomType(RoomType type)
    {
        switch (type)
        {
            case RoomType.Combat:
                return combatRooms[UnityEngine.Random.Range(0, combatRooms.Count)];
            case RoomType.Treasure:
                return treasureRooms[UnityEngine.Random.Range(0, treasureRooms.Count)];
            case RoomType.Event:
                return eventRooms[UnityEngine.Random.Range(0, eventRooms.Count)];
            case RoomType.Shop:
                return shopRooms[UnityEngine.Random.Range(0, shopRooms.Count)];
            case RoomType.Boss:
                return bossRoom;
            default:
                return combatRooms[0];
        }
    }

    private RoomContent GenerateRoomContent(RoomType type, int difficulty)
    {
        switch (type)
        {
            case RoomType.Combat:
                return GenerateCombatContent(difficulty);
            case RoomType.Treasure:
                return GenerateTreasureContent(difficulty);
            case RoomType.Event:
                return GenerateEventContent();
            case RoomType.Shop:
                return GenerateShopContent(difficulty);
            case RoomType.Boss:
                return GenerateBossContent(difficulty);
            default:
                return null;
        }
    }

    // 生成战斗房间内容，根据难度调整
    private CombatRoomContent GenerateCombatContent(int difficulty)
    {
        CombatRoomContent content = new CombatRoomContent();

        // 根据难度生成不同类型和数量的敌人
        int enemyCount = UnityEngine.Random.Range(2 + difficulty, 5 + difficulty);

        for (int i = 0; i < enemyCount; i++)
        {
            // 随机选择敌人类型
            EnemyType enemyType = GetRandomEnemyType(difficulty);
            content.enemies.Add(enemyType);
        }

        return content;
    }

    private EnemyType GetRandomEnemyType(int difficulty)
    {
        // 根据难度返回合适的敌人类型
        Array enemyTypes = Enum.GetValues(typeof(EnemyType));
        
        // 简单难度：只生成基础敌人
        if (difficulty <= 1)
        {
            return (EnemyType)enemyTypes.GetValue(UnityEngine.Random.Range(0, 2));
        }
        // 中等难度：可以生成更强大的敌人
        else if (difficulty <= 3)
        {
            return (EnemyType)enemyTypes.GetValue(UnityEngine.Random.Range(0, enemyTypes.Length - 1));
        }
        // 高难度：可以生成所有类型的敌人，包括Boss
        else
        {
            return (EnemyType)enemyTypes.GetValue(UnityEngine.Random.Range(0, enemyTypes.Length));
        }
    }

    private TreasureRoomContent GenerateTreasureContent(int difficulty)
    {
        TreasureRoomContent content = new TreasureRoomContent();
        
        // 根据难度生成不同价值的宝物
        int treasureCount = UnityEngine.Random.Range(1, 3 + difficulty);
        // 由于 TreasureRoomContent 未定义 treasureValue，此处注释掉该代码，实际需根据 TreasureRoomContent 定义修改
        // content.treasureValue = treasureCount * 10;
        
        return content;
    }

    private EventRoomContent GenerateEventContent()
    {
        EventRoomContent content = new EventRoomContent();
        
        // 随机生成事件类型
        Array eventTypes = Enum.GetValues(typeof(EventType));
        content.eventType = (EventType)eventTypes.GetValue(UnityEngine.Random.Range(0, eventTypes.Length));
        
        return content;
    }

    private ShopRoomContent GenerateShopContent(int difficulty)
    {
        ShopRoomContent content = new ShopRoomContent();
        
        // 根据难度调整商店商品数量和质量
        content.itemCount = UnityEngine.Random.Range(3, 6 + difficulty);
        content.basePriceMultiplier = 1f + (difficulty * 0.1f);
        
        return content;
    }

    private BossRoomContent GenerateBossContent(int difficulty)
    {
        BossRoomContent content = new BossRoomContent();
        
        // 根据难度选择Boss类型
        Array bossTypes = Enum.GetValues(typeof(BossType));
        content.bossType = (BossType)bossTypes.GetValue(UnityEngine.Random.Range(0, bossTypes.Length));
        content.bossLevel = difficulty;
        
        return content;
    }
}
